package com.rt.easyjava.base.biz1.controller.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rt.easyjava.base.biz1.bean.TmptAuthDeptBiz;
import com.rt.easyjava.base.biz1.dto.TmptAuthDeptBizDto;
import com.rt.easyjava.base.biz1.service.intf.TmptAuthDeptBizService;
import com.rt.tmpt.service.utils.SpringContextHolder;
import com.rt.easyjava.common.base.MyQueryWrapper;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.base.UserDetailsDto;
import com.rt.easyjava.common.config.dozer.MyMapper;
import com.rt.easyjava.common.exception.ControllerException;
import com.rt.easyjava.common.utils.others.DateTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;

import java.lang.reflect.Method;
import java.util.List;

/**
 * 前端控制器
 * @author wuxiaoyong
 * @since 2020-11-28
 * 用户信息可直接形参获取：String (_userId，_loginName，_userName，_companyId，_userJson) or userDetailsDto.getUserId()
 */
@Slf4j
public class TmptAuthDeptController extends ControllerException {
    @Autowired
    protected MyMapper mapper;
    @Autowired
    protected TmptAuthDeptBizService tmptAuthDeptBizService;


    protected Result page(TmptAuthDeptBizDto tmptAuthDeptBizDto, UserDetailsDto userDetailsDto){
        Result<TmptAuthDeptBizDto> result = new Result<>();

        Page<TmptAuthDeptBiz> page = new Page<>(tmptAuthDeptBizDto.getPage(), tmptAuthDeptBizDto.getLimit());

        // 查询条件
        TmptAuthDeptBiz tmptAuthDeptBizQuery = mapper.map(tmptAuthDeptBizDto, TmptAuthDeptBiz.class);
        // set deleteflag default 0, 查询参数时，默认只查有效数据
        tmptAuthDeptBizQuery.setDeleteflagDefaultZero();
        // 数据访问边界 0：全部数据， 10：机构内部数据
        tmptAuthDeptBizQuery.rebuildDataLimitQueryBean(userDetailsDto);

        QueryWrapper<TmptAuthDeptBiz> queryWrapper=new QueryWrapper<>(tmptAuthDeptBizQuery);
        //特殊查询条件
        queryWrapper.orderBy(tmptAuthDeptBizDto.getCondition(), tmptAuthDeptBizDto.isAsc(), tmptAuthDeptBizDto.getColumn());

        return tmptAuthDeptBizService.page(page, queryWrapper);
    }

    /**
        树结构查询方法，非树结构会造一个空方法备用着。
    */
    protected Result tree (Long id) {
        Result<TmptAuthDeptBizDto> result = new Result<>();
        // when id=0 , id=0 is parantId=0 , 默认设置一个空根
        TmptAuthDeptBizDto tmptAuthDeptBizDto = new TmptAuthDeptBizDto();
        tmptAuthDeptBizDto.setId(0L);
        if (id!=0) {
            TmptAuthDeptBiz tmptAuthDeptBiz = tmptAuthDeptBizService.getById(id);
            tmptAuthDeptBizDto = mapper.map(tmptAuthDeptBiz, TmptAuthDeptBizDto.class);
        }
        treeReduce(tmptAuthDeptBizDto);
        result.setObj(tmptAuthDeptBizDto);

        return result.success();
    }

    private void treeReduce(TmptAuthDeptBizDto tmptAuthDeptBizDto){
        // myson list
        QueryWrapper<TmptAuthDeptBiz> queryWrapper=new QueryWrapper<>();
        // queryWrapper.eq("tmpt_auth_dept_id", tmptAuthDeptBizDto.getId());
        queryWrapper.eq("parent_id", tmptAuthDeptBizDto.getId());
        queryWrapper.eq("deleteflag", 0);
        queryWrapper.orderByAsc(true, "id");
        List<TmptAuthDeptBiz> sonList = tmptAuthDeptBizService.list(queryWrapper);
        if (sonList.size() == 0) {
            return;
        }
        List<TmptAuthDeptBizDto> sonDtoList = mapper.maplist(sonList, TmptAuthDeptBizDto.class);

        tmptAuthDeptBizDto.setTmptAuthDeptList(sonDtoList);
        for (TmptAuthDeptBizDto dto : sonDtoList) {
            treeReduce(dto);
        }
    }

    protected Result myPage(TmptAuthDeptBizDto tmptAuthDeptBizDto, UserDetailsDto userDetailsDto){
        Result<TmptAuthDeptBizDto> result = new Result<>();

        Page<TmptAuthDeptBiz> page = new Page<>(tmptAuthDeptBizDto.getPage(), tmptAuthDeptBizDto.getLimit());

        // 查询条件
        TmptAuthDeptBiz tmptAuthDeptBizQuery = mapper.map(tmptAuthDeptBizDto, TmptAuthDeptBiz.class);
        // set deleteflag default 0, 查询参数时，默认只查有效数据
        tmptAuthDeptBizQuery.setDeleteflagDefaultZero();
        // 数据访问边界 0：全部数据， 10：机构内部数据
        tmptAuthDeptBizQuery.rebuildDataLimitQueryBean(userDetailsDto);

        // 多表查询时，plus自带不支持，这里封装一个工具类处理，mapper.xml中主表别名是t1, 给所有非空字段加别名； 特殊查询条件，在工具类中要忽略掉，最后自定义增加。
        QueryWrapper<TmptAuthDeptBiz> queryWrapper = MyQueryWrapper.buildQueryWrapper(tmptAuthDeptBizQuery );
        //特殊查询条件
        queryWrapper.orderBy(tmptAuthDeptBizDto.getCondition(), tmptAuthDeptBizDto.isAsc(), "t1."+tmptAuthDeptBizDto.getColumn());

        result = tmptAuthDeptBizService.myPage(page, queryWrapper);

        return result;
    }

    protected Result selectBy(TmptAuthDeptBizDto tmptAuthDeptBizDto, UserDetailsDto userDetailsDto){
        Result<TmptAuthDeptBizDto> result = new Result<>();

        // 查询条件
        TmptAuthDeptBiz tmptAuthDeptBizQuery = mapper.map(tmptAuthDeptBizDto, TmptAuthDeptBiz.class);
        // set deleteflag default 0, 查询参数时，默认只查有效数据
        tmptAuthDeptBizQuery.setDeleteflagDefaultZero();
        // 数据访问边界 0：全部数据， 10：机构内部数据
        tmptAuthDeptBizQuery.rebuildDataLimitQueryBean(userDetailsDto);

        QueryWrapper<TmptAuthDeptBiz> queryWrapper=new QueryWrapper<>(tmptAuthDeptBizQuery);
        //特殊查询条件
        queryWrapper.orderBy(tmptAuthDeptBizDto.getCondition(), tmptAuthDeptBizDto.isAsc(), tmptAuthDeptBizDto.getColumn());

        result = tmptAuthDeptBizService.selectBy(queryWrapper);

        return result;
    }

    protected Result selectById(Long id){
        //调用服务
        return tmptAuthDeptBizService.selectById(id);
    }

    protected Result select4column(String columnName){
        try {
            // vue默认传过来的，出于枚举类名考虑，首字母是大写，这样要小字当属性用。
            columnName = columnName.substring(0,1).toLowerCase()+columnName.substring(1);
            Object o = SpringContextHolder.getBean("tmptAuthDeptService");
            Method method = o.getClass().getMethod("mySelect4"+columnName);
            return Result.builder(method.invoke(o)).success();
        }catch (Exception e){
            throw new RuntimeException("方法名不存在");
        }
    }

    protected Result create(@RequestBody TmptAuthDeptBizDto tmptAuthDeptBizDto,
                    String _userId, String _userName, String _companyId){
        //插入参数
        tmptAuthDeptBizDto.setCreatemanId(Long.parseLong(_userId));
        tmptAuthDeptBizDto.setCreateman(_userName);
        tmptAuthDeptBizDto.setCreatedate(DateTool.getCurrentDatetimeStr());
        tmptAuthDeptBizDto.setCompanyId(Long.parseLong(_companyId));

        //调用服务
        return tmptAuthDeptBizService.create(tmptAuthDeptBizDto);
    }

    protected Result update(@RequestBody TmptAuthDeptBizDto tmptAuthDeptBizDto,
                        String _userId, String _userName){
        //参数
        tmptAuthDeptBizDto.setModifymanId(Long.parseLong(_userId));
        tmptAuthDeptBizDto.setModifyman(_userName);
        tmptAuthDeptBizDto.setModifydate(DateTool.getCurrentDatetimeStr());
        tmptAuthDeptBizDto.setCreatedate(null);
        tmptAuthDeptBizDto.setCreateman(null);
        tmptAuthDeptBizDto.setCreatemanId(null);
        tmptAuthDeptBizDto.setCompanyId(null);

        //query condition for update
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", tmptAuthDeptBizDto.getId()==null?"0":tmptAuthDeptBizDto.getId());

        //调用服务
        return tmptAuthDeptBizService.update(tmptAuthDeptBizDto, updateWrapper);
    }

    protected Result delete(Long id, String _userId, String _userName){
        Result result = new Result();

        //参数
        TmptAuthDeptBizDto tmptAuthDeptBizDto = new TmptAuthDeptBizDto();
        tmptAuthDeptBizDto.setModifymanId(Long.parseLong(_userId));
        tmptAuthDeptBizDto.setModifyman(_userName);
        tmptAuthDeptBizDto.setModifydate(DateTool.getCurrentDatetimeStr());
        tmptAuthDeptBizDto.setDeleteflag("1");

        //query condition for update
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id==null?0:id.longValue());

        //调用服务
        return tmptAuthDeptBizService.update(tmptAuthDeptBizDto, updateWrapper);
    }

    protected Result deletePhysical(Long id){
        Result result = new Result();

        //调用服务
        Boolean aBoolean =  false;//tmptAuthDeptBizService.removeById(id==null?0:id.longValue());
        if(aBoolean){
            result.success();
        }else {
            result.fault();
        }

        return result;
    }

}
